氚云平台中,用php代码调取api数据!

2,194次阅读
一条评论

php代码调用氚云平台的数据!

废话就不说了,直接止代码!

<?php
// 请求地址
$apiAddress = "https://www.h3yun.com/OpenApi/Invoke";

// 认证参数(请填写你的实际 EngineCode 和 EngineSecret)
$engineCode = "";           // 氚云的 EngineCode
$engineSecret = "";       // 氚云的 EngineSecret

// 获取当天日期,格式 yyyy/m/d
$today = date('Y/n/j');
// 请求参数数组
$params = [
    "ActionName" => "LoadBizObjects",
    "SchemaCode" => "",     // 氚云的 SchemaCode
    "Filter" => json_encode([
        "FromRowNum" => 0,
        "RequireCount" => false,
        "ReturnItems" => [],
        "SortByCollection" => [],
        "ToRowNum" => 500,
        "Matcher" => [
            "Type" => "And",
            "Matchers" => []
        ]
    ])
];

// JSON 编码请求参数
$jsonData = json_encode($params);
// 初始化 cURL
$ch = curl_init($apiAddress);
// 设置 HTTP 请求头
$headers = [
    "Content-Type: application/json",
    "EngineCode: $engineCode",
    "EngineSecret: $engineSecret"
];

// 设置 cURL 选项
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData);

// 发送请求并获取响应
$response = curl_exec($ch);
if (curl_errno($ch)) {
    echo "cURL Error: " . curl_error($ch);
} else {
    $data = json_decode($response, true);
    if ($data["Successful"] && isset($data["ReturnData"]["BizObjectArray"])) {
        $items = $data["ReturnData"]["BizObjectArray"];

        // 按来访时间排序,降序(越早的时间越靠后)
        usort($items, function($a, $b) {
            $timeA = $a["F0000047"] ?? '';      // F0000047 为字段的编号
            $timeB = $b["F0000047"] ?? '';
            // 如果时间格式是标准可比较字符串,直接比较
            return strcmp($timeB,$timeA);
        });
        $result = [];
        foreach ($items as $item) {
            if (strpos($item["F0000047"], $today) === 0) {      // 只显示当天的信息,可以根据需要修改。
                $result[] = [
                    "名称" => $item["F0000036"] ?? "无",      // F0000036 为字段的编号
                    "日期" => $item["F0000047"] ?? "无",
                    "等级" => $item["F0000072"] ?? "无",
                ];

            }
        }
        json_encode($result, true);
    } else {
        echo "请求失败或无数据";
    }
}

// 关闭 cURL 资源
curl_close($ch);
沛霖主页

共计17人点赞,其中1人来自小程序

正文完
微信扫码打开小程序体验更多功能
post-qrcode
 17
沛霖主页
版权声明:本站原创文章,由 沛霖主页 2025-05-21发表,共计1577字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(一条评论)
Harvey Waters 评论达人 LV.1
2025-07-08 22:18:19 回复

Your writing is a true testament to your expertise and dedication to your craft. I’m continually impressed by the depth of your knowledge and the clarity of your explanations. Keep up the phenomenal work!

   shanks  法国Loiret橘子电信